package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.car.controller.CarCreationEasyInputController;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang
 * 立体停车场入库action
 */
public class CarCreationEasyInputAction extends TmsAction{
	private Logger logger = Logger.getLogger(CarCreationEasyInputAction.class);
	private static final String BACKFLAG = "backFlag"; 
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
		
		if ("ok".equals(uc.getAttribute("result"))) {
			uc.removeAttribute("result");
			carCreationInputViewHelper = (CarCreationInputViewHelper)uc.getAttribute(VIEWHELP);
			request.setAttribute(VIEWHELP, carCreationInputViewHelper);	
		} else {
			CarCreationEasyInputController controller  = new CarCreationEasyInputController();
			controller.execute(uc);
			carCreationInputViewHelper = (CarCreationInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			request.setAttribute(VIEWHELP, carCreationInputViewHelper);	
			
			if ("err".equals(uc.getAttribute("vinCheckError"))) {
				
				String shortName = (String)uc.getAttribute("shortName");
				String[] param = {shortName};
				setErrorsToRequest(request, TmsConstants.U8007, param); // 输入的车架号已经被【{0}】登记使用，不能重复登记。
				uc.removeAttribute("vinCheckError");
				uc.removeAttribute("memberId");
				return mapping.getInputForward();
			}
			
			if ("err1".equals(uc.getAttribute("vinCheckError"))) {

				setErrorsToRequest(request, TmsConstants.U8004, null); // 当前商户的允许登记数量已经用完，无法进行保存。
				uc.removeAttribute("vinCheckError");
				uc.removeAttribute("memberId");
				return mapping.getInputForward();
			}
			
			if ("err2".equals(uc.getAttribute("vinCheckError"))) {

				setErrorsToRequest(request, TmsConstants.U8006, null); // 输入的商户名称不存在。
				uc.removeAttribute("vinCheckError");
				uc.removeAttribute("memberId");
				return mapping.getInputForward();
			}
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
